package com.server.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.ISurveyAnalyzeService;
import com.server.service.ISurveyTestService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

/**
 * 调研试卷分析
 * @author wufeng
 * @date 创建时间：2016年9月5日 下午3:24:10 
 */
@Controller
@RequestMapping("surveyAnalyze")
public class SurveyAnalyzeController {
	
	@Autowired
	private ISurveyAnalyzeService surveyAnalyzeService;
	@Autowired 
	private ISurveyTestService surveyTestService;
	
	/**
	* 调研统计页面
	* @author  作者 : wufeng
	* @date 创建时间：2016年9月7日 下午3:59:07 
	* @return  
	*/
	@RequestMapping("mv/{paperId}")
	public ModelAndView mv(@PathVariable String paperId){
		 ModelAndView mv = new ModelAndView("survey/statistics");
		 mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		 mv.addObject("paperId",paperId);
		 return mv;
	}
	/**
	* 调研问卷作答页面
	* @author  作者 : wufeng
	* @date 创建时间：2016年9月7日 下午3:58:13 
	* @return  
	*/
	@RequestMapping("answerMv/{paperId}")
	public ModelAndView answerMv(HttpServletRequest request,@PathVariable String paperId){
		ModelAndView mv = null;
		String uuid = SessionUtil.getStringAttri(request, SessionData.uuidKey);
		if(surveyAnalyzeService.getState(paperId, uuid) == 0){
			mv = new ModelAndView("survey/respondence");
			mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		}else{
			mv = new ModelAndView("survey/respondenceEd");
		}
		return mv;
	}
	
	/**
	* 保存用户填写的调研数据
	* 
	* @author  作者 : wufeng
	* @date 创建时间：2016年9月7日 下午4:04:29 
	* @return  
	*/
	@RequestMapping("addData/{paperId}")
	public void addData(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String paperId){
		String json = request.getParameter("data");
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey);
		String user_uuid = SessionUtil.getStringAttri(request, SessionData.uuidKey);
		Map<String, Object> map = JSONSerielizer.toMap(json);
		boolean isSuccess = surveyAnalyzeService.saveMap(user, map,paperId,user_uuid);
		OutputUtil.returnSuccess(response, isSuccess);
		
	}
	/**
	* 获取paperid 问卷数据
	* @author  作者 : wufeng
	* @date 创建时间：2016年9月7日 下午3:58:41 
	* @return  
	*/
	@RequestMapping("mapData/{paperId}")
	public void getMapData(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String paperId){
		Map<String, Object> map = surveyAnalyzeService.getMap(paperId);
		String jsonString = JSONSerielizer.toJson(map, null, null);
		OutputUtil.returnString(response, jsonString);
	}
	
	@RequestMapping("listData/{paperId}")
	public void getListData(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String paperId){
		List<Map<String, Object>> reList = 
				new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> questionList = surveyAnalyzeService.getTestList(paperId,"1");
		List<Map<String, Object>> answerList = surveyAnalyzeService.getAnswerList(paperId);
		int number = questionList.size();
		if(number>0){
			for(int i = 0; i<number;i++){
				Map<String, Object> reMap = new HashMap<String, Object>();
				Map<String, Object> testMap = new HashMap<String, Object>();
				testMap = questionList.get(i);
				String classType = testMap.get("class_id").toString();
				if(classType.equals("单选题")){
					String selects = testMap.get("selects").toString();
					String select[] = selects.split(",nwebadjj,_");
					String options = null;
					for(int j = 0; j< select.length;j++){
						char option = (char)(j+65);
						if(options==null){
							options = String.valueOf(option);
						}else{
							options += "," + option;
						}
					}
					String _option[] = options.split(",");
					int value[] = new int[_option.length];
					for (int j = 0; j < _option.length; j++) {
						value[j]=0;
					}
					for(int k = 0; k<answerList.size();k++){
						String answers = null;
						String answer[] = new String[1000];
						Map<String, Object> answerMap = new HashMap<String, Object>();
						answerMap = answerList.get(k);
						answers = answerMap.get("answer").toString();
						answer = answers.split("_kahsdkf_fsd!~&\\*_");
						for (int j = 0; j < _option.length; j++) {
							if(answer[i].equals(_option[j])){
								value[j]++;
							}
						}
					} 
					for (int j = 0; j < _option.length; j++) {
						reMap.put(_option[j], value[j]);
					}
					testMap.put("data", reMap);
					reList.add(testMap);
				}else if(classType.equals("多选题")){
					String selects = testMap.get("selects").toString();
					String select[] = selects.split(",nwebadjj,_");
					String options = null;
					for(int j = 0; j< select.length;j++){
						char option = (char)(j+65);
						if(options==null){
							options = String.valueOf(option);
						}else{
							options += "," + option;
						}
					}
					String _option[] = options.split(",");
					int value[] = new int[_option.length];
					for (int j = 0; j < _option.length; j++) {
						value[j]=0;
					}
					for(int k = 0; k<answerList.size();k++){
						String answers = null;
						String answer[] = new String[1000];
						Map<String, Object> answerMap = new HashMap<String, Object>();
						answerMap = answerList.get(k);
						answers = answerMap.get("answer").toString();
						answer = answers.split("_kahsdkf_fsd!~&\\*_");
						for (int j = 0; j < _option.length; j++) {
							if(answer[i].contains(_option[j])){
								value[j]++;
							}
						}
					} 
					for (int j = 0; j < _option.length; j++) {
						reMap.put(_option[j], value[j]);
					}
					testMap.put("data", reMap);
					reList.add(testMap);
				}else if(classType.equals("判断题")){
					int right = 0;
					int wrong = 0;
					for(int k = 0; k<answerList.size();k++){
						String answers = null;
						String answer[] = new String[1000];
						Map<String, Object> answerMap = new HashMap<String, Object>();
						answerMap = answerList.get(k);
						answers = answerMap.get("answer").toString();
						answer = answers.split("_kahsdkf_fsd!~&\\*_");
						if(answer[i].equals("true")){
							right++;
						}else{
							wrong++;
						}
					} 
					reMap.put("right", right);
					reMap.put("wrong", wrong);
					testMap.put("data", reMap);
					reList.add(testMap);
				}else{
					reList.add(testMap);
				}
			}
		}
		String jsonString = JSONSerielizer.toJson(reList, null, null);
		OutputUtil.returnString(response, jsonString);
	}
	
	@RequestMapping("solutionListMv/{paperId}/{order}")
	public ModelAndView solutionListMv(@PathVariable String paperId,@PathVariable String order){
		ModelAndView mv = new ModelAndView("survey/solutionAnswerList");
		 mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		 mv.addObject("paperId",paperId);
		 mv.addObject("order",order);
		 return mv;
	}
	
	@RequestMapping("solutionListData/{paperId}/{order}")
	public void solutionListData(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String paperId,@PathVariable String order){
		DataVector<Map<String, Object>> listData =  surveyAnalyzeService.
				getSolutionAnswerList(paperId, order, new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(),
				JSONSerielizer.toJson(listData.getList(),null,null));
	}
}
